﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WindowsGame1
{
    public class Explosion
    {
        public List<Particle> particles = new List<Particle>();
        Sprite spr = new Sprite();

        public Explosion()
        {
            spr.origin = new Vector2(0.5f, 0.5f);
        }

        public bool Update(Timer t)
        {
            for (int i = 0; i < particles.Count; i++)
            {
                Particle p = particles[i];
                if (p.life > 0)
                {
                    p.pos += p.vel * t.Delta;
                    p.vel += p.acc * t.Delta;
                    p.size += p.sizeStep * t.Delta;
                    p.color += p.colorStep * t.Delta;
                    p.color = Vector4.Clamp(p.color, Vector4.Zero, new Vector4(1, 1, 1, 1));
                    p.life -= t.Delta;
                }
                else
                {
                    particles.RemoveAt(i--);
                }
            }

            return particles.Count > 0;
        }

        public void Draw(SpriteBatch sb)
        {
            foreach (Particle p in particles)
            {
                spr.pos = p.pos;
                spr.color = new Color(p.color);
                spr.size = new Vector2(p.size, p.size);
                spr.Draw(sb);
            }
        }
    }
}
